<?php


namespace App\Services;


use App\Tools\JsonTools;
use App\Tools\RaffledetailsTools;
use App\Tools\RedisTools;
use Illuminate\Support\Facades\Log;

class IntegralService
{
            public $redis;
            public function __construct()
            {
                $this->redis=RedisTools::getInstance()->connectRedis();
            }

            /**
             * @Notes:
             * @Author: 遇见
             * @Date: 2022/11/24
             * @Time: 13:45
             * @param $uid
             * @param $param
             * @param $integral
             * @return array
             * 抽奖
             */
            public function IntegralServices($uid,$param,$integral){
                $integrals=$this->redis->hget('Integral',$uid);
                if(!empty($integrals)) return JsonTools::JsonError([],2001,'失败抽过了');
                $data=[];//奖品级别
                for ($i=0;$i<=$param;$i++){
                    $data[]=['id'=>$i,'data'=>$i."等奖",'v'=>$i];
                }
                $arr = [];
                foreach ($data as $key => $val) {
                    $arr[$val['id']] = $val['v'];
                }
                // 中奖 id
                $rid = $this->get_rand($arr);
                $data=[];
                if($rid){
                    foreach ($integral as $k=>$value){
                        if($value['id']==$rid){
                            $data[]=[
                              'uid'=>$uid,
                              'prize'=>$value['integral'],//积分
                              'details'=>$rid."等奖励"
                            ];
                        }
                    }
                }
                //处理用户中奖信息
                $toole = (new RaffledetailsTools())->raff($data,$rid);
                if(!empty($toole)){
                    $this->redis->hset('Integral',$uid,1);
                    return $toole;
                }
//                return JsonTools::JsonError([],2001,'抽奖失败');
            }

            /**
             * @Notes:
             * @Author: 遇见
             * @Date: 2022/11/24
             * @Time: 13:45
             * @param array $proArr
             * @return int|string
             * 轮询抽奖几率
             */
            public function get_rand($proArr = array()) {
                if(empty($proArr)) die;
                $rid = '';

                // 概率数组的总权重
                $proSum = array_sum($proArr);

                // 概率数组循环
                foreach ($proArr as $k => $proCur) {
                    // 从 1 到概率总数中任意取值
                    $randNum = mt_rand(1, $proSum);
                    // 判断随机数是否在概率权重中
                    if ($randNum <= $proCur) {
                        // 取出奖品 id
                        $rid = $k;
                        break;
                    } else {
                        // 如果随机数不在概率权限中，则不断缩小总权重，直到从奖品数组中取出一个奖品
                        $proSum -= $proCur;
                    }
                }

            unset($proArr);
            return $rid;
         }

        /**
         * @Notes:
         * @Author: 遇见
         * @Date: 2022/11/24
         * @Time: 13:58
         * @return array
         * 清空整个签到Hash表
         */
         public function redisHashDel(){
             $integrals=$this->redis->delete('Integral');
             if($integrals){
                 return JsonTools::JsonSuccess();
             }
         }
}
